package com.ty.zls.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUIPagerAdapter;
import com.qmuiteam.qmui.widget.QMUIWindowInsetLayout;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ty.zls.R;
import com.ty.zls.bean.Water;

import java.util.LinkedList;
import java.util.List;

/**
 *  抄表pager适配器
 */
public class WaterPagerAdapter extends QMUIPagerAdapter {


    private Context context;
    private List<Water> waters = new LinkedList<>();
    QMUICommonListItemView userId;
    QMUICommonListItemView name;
    QMUICommonListItemView tel;
    QMUICommonListItemView area;
    QMUICommonListItemView lastTime;
    QMUICommonListItemView lastScale;
    QMUICommonListItemView sort;

    public List<Water> getWaters() {
        return waters;
    }

    public void setWaters(List<Water> waters) {
        this.waters = waters;
    }

    public WaterPagerAdapter(Context context){
        this.context = context;
    }

    @Override
    protected Object hydrate(ViewGroup container, int position) {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        QMUIWindowInsetLayout root = (QMUIWindowInsetLayout) inflater.inflate(R.layout.water_add_item, container, false);
        root.setRadiusAndShadow(QMUIDisplayHelper.dp2px(context, 15),
                QMUIDisplayHelper.dp2px(context, 14), 0.25f);
        createGroup(root);
        return root;
    }


    private void createGroup(View root){
        QMUIGroupListView listView = root.findViewById(R.id.qg_water_add_list);
        userId = listView.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.example_image0),
                "用户编号",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE
        );

        name = listView.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.example_image0),
                "用户名",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE
        );

        tel = listView.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.example_image0),
                "手机号",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CUSTOM
        );
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        QMUIRoundButton editTel = new QMUIRoundButton(context);
        editTel.setText("修改");
        editTel.setLayoutParams(params);
        editTel.setTextColor(context.getResources().getColor(R.color.app_color_theme_5));
        editTel.setClickable(true);
        int padding = QMUIDisplayHelper.dp2px(context, 10);
        editTel.setPadding(padding,padding,padding,padding);
        tel.addAccessoryCustomView(editTel);

        area = listView.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.example_image0),
                "用水地址",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE
        );

        lastTime = listView.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.example_image0),
                "上期日期",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE
        );

        lastScale = listView.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.example_image0),
                "上期指数",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE
        );


        sort = listView.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.example_image0),
                "排序号",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CUSTOM
        );
        QMUIRoundButton editSort = new QMUIRoundButton(context);
        editSort.setText("修改");
        ViewGroup.LayoutParams params1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        editSort.setTextColor(context.getResources().getColor(R.color.app_color_theme_5));
        editSort.setLayoutParams(params1);
        editSort.setClickable(true);
        editSort.setPadding(padding,padding,padding,padding);
        sort.addAccessoryCustomView(editSort);


        int size = QMUIDisplayHelper.dp2px(context, 20);
        QMUIGroupListView.newSection(context)
                .setTitle("用户信息")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(userId, null)
                .addItemView(name, null)
                .addItemView(tel, null)
                .addItemView(lastTime, null)
                .addItemView(lastScale, null)
                .addItemView(sort,null)
                .addTo(listView);

        QMUIGroupListView.newSection(context)
                .setTitle("抄表信息")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addTo(listView);


    }

    @Override
    protected void populate(ViewGroup container, Object item, int position) {
        QMUIWindowInsetLayout itemView = (QMUIWindowInsetLayout) item;
        Water water = waters.get(position);
        name.setDetailText(water.getName());
        userId.setDetailText(water.getId());
        tel.setDetailText(water.getTel());
        sort.setDetailText(water.getSort());
        area.setDetailText(water.getArea());
        lastScale.setDetailText(water.getLastScale());
        lastTime.setDetailText(water.getLastTime());
        container.addView(itemView);
    }

    @Override
    protected void destroy(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return waters.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

}
